﻿using MediatR;
using MS.BasicData.Domain;
using MS.BasicData.Infrastructure;
using MS.BasicData.Infrastructure.IBaseRepository;
using System.ComponentModel.DataAnnotations;

namespace MS.BasicData.Read.API.Applications.Permissionss
{
    public class UpdatePermiRequestHandler : IRequestHandler<UpdatePermiRequestModel, ApiRestult>
    {
        private readonly IPermisBaseBaseRepository _permissionsRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="permissionsRepository"></param>
        public UpdatePermiRequestHandler(IPermisBaseBaseRepository permissionsRepository)
        {
            _permissionsRepository = permissionsRepository;
        }

        public async Task<ApiRestult> Handle(UpdatePermiRequestModel request, CancellationToken cancellationToken)
        {
            ApiRestult restult = new ApiRestult
            {
                isSuc = true,
                Message = "更新角色成功",
                Code = Restult.Success
            };

            var permis = await _permissionsRepository.GetAll(a=>a.PermissionId == request.PermissionsId);

            permis.PermissionId = request.PermissionsId;
            permis.PermissionsName = request.PermissionsName;

            await _permissionsRepository.UpdateAsync(permis);

            return restult;
        }
    }


    public class UpdatePermiRequestModel : IRequest<ApiRestult>
    {
        /// <summary>
        /// 权限Id
        /// </summary>
        public int PermissionsId { get; set; }
        /// <summary>
        /// 权限名称
        /// </summary>
        public string PermissionsName { get; set; } = string.Empty;
    }
}
